package com.fq.demo.config;

import com.fq.demo.service.ToolsService;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.service.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author 超chao
 * @Description TODO
 * @Date 2025/3/21/周五 11:55
 * @Version 1.0
 */
@Configuration
public class AiConfig {

    public interface Assistant {
        String chat(String message);
        // 流式相遇
        TokenStream chatStream(String message);

        @SystemMessage("""
                            您是“顺路达”的客户聊天代理智能客服，请以友好、乐于助人且愉快的方式来回复。
                            您正在通过在线聊天系统与客户互动。
                            开头先介绍自己。
                            请讲中文。
                            今天的日期是 {{current_data}}.
                            """)
        TokenStream chatStream(@UserMessage String message,@V("current_data") String currentData);
    }

    @Bean
    public Assistant assistant(ChatLanguageModel languageModel,
                               StreamingChatLanguageModel streamingLanguageModel,
                               ToolsService toolsService){
        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);

        Assistant assistant = AiServices
                .builder(Assistant.class)
                .tools(toolsService)
                .chatLanguageModel(languageModel)
                .streamingChatLanguageModel(streamingLanguageModel)
                .chatMemory(chatMemory)
                .build();
        return assistant;

    }

    public interface AssistantUnique {
        String chat(@MemoryId int memoryId,@UserMessage String message);
        // 流式相遇
        TokenStream chatStream(@MemoryId int memoryId,@UserMessage String message);
    }

    @Bean
    public AssistantUnique assistantUnique(ChatLanguageModel languageModel, StreamingChatLanguageModel streamingLanguageModel){

        AssistantUnique assistant = AiServices
                .builder(AssistantUnique.class)
                .chatLanguageModel(languageModel)
                .streamingChatLanguageModel(streamingLanguageModel)
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.builder().maxMessages(10).id(memoryId).build())
                .build();
        return assistant;
    }

    public interface AssistantUniqueStore {
        String chat(@MemoryId int memoryId,@UserMessage String message);
        // 流式相遇
        TokenStream chatStream(@MemoryId int memoryId,@UserMessage String message);
    }

    @Bean
    public AssistantUnique assistantUniqueStore(ChatLanguageModel languageModel, StreamingChatLanguageModel streamingLanguageModel){
        PersistentChatMemoryStore store = new PersistentChatMemoryStore();
        ChatMemoryProvider memoryProvider = memoryId -> MessageWindowChatMemory
                .builder()
                .id(memoryId)
                .maxMessages(10)
                .chatMemoryStore(store)
                .build();

        AssistantUnique assistant = AiServices
                .builder(AssistantUnique.class)
                .chatLanguageModel(languageModel)
                .streamingChatLanguageModel(streamingLanguageModel)
                .chatMemoryProvider(memoryProvider)
                .build();
        return assistant;
    }
}
